package h0730;

import java.util.*;

/**
  * @description 约瑟夫问题
  * @author 不知名帅哥
  * @date 2024/7/30 22:29
  * @version 1.0
*/
public class JosephCycle {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int[] input = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int len=sc.nextInt();
        int m=sc.nextInt();//目标
        getRes(input,m);

    }

    //使用双端队列来模拟循环链表
    private static void getRes(int[] input, int m) {
        //定义循环链表
        LinkedList<Integer> cycleList=new LinkedList<>();
        for (int i : input) {
            cycleList.add(i);
        }
        StringJoiner res=new StringJoiner(",");
        int count=0;
        while (!cycleList.isEmpty()){
            count++;
            Integer head = cycleList.removeFirst();
            if (count==m){
                count=0; //重置计数器
                m=head; //新的目标数
                res.add(head+"");
            }else {
                cycleList.addLast(head); //重新插回末尾
            }
        }
        System.out.println(res);
    }
}
